// const API_BASE_URL = 'http://localhost:3000/api'

class AdminService {
  async getProducts(token) {
    await new Promise(resolve => setTimeout(resolve, 500))
    
    if (!token) {
      throw new Error('未授权访问')
    }
    
    // 模拟商品数据
    return [
      { id: 1, name: 'iPhone 15', price: 5999, stock: 50, category: '手机', status: 'active', createdAt: '2024-01-01' },
      { id: 2, name: 'MacBook Pro', price: 12999, stock: 20, category: '电脑', status: 'active', createdAt: '2024-01-02' },
      { id: 3, name: 'AirPods Pro', price: 1899, stock: 100, category: '配件', status: 'inactive', createdAt: '2024-01-03' },
      { id: 4, name: 'iPad Air', price: 4399, stock: 30, category: '平板', status: 'active', createdAt: '2024-01-04' }
    ]
  }

  async createProduct(token, productData) {
    await new Promise(resolve => setTimeout(resolve, 500))
    
    if (!token) {
      throw new Error('未授权访问')
    }
    
    const newProduct = {
      id: Date.now(),
      ...productData,
      createdAt: new Date().toISOString(),
      status: 'active'
    }
    
    return newProduct
  }

  async updateProduct(token, productId, productData) {
    await new Promise(resolve => setTimeout(resolve, 500))
    
    if (!token) {
      throw new Error('未授权访问')
    }
    
    return {
      ...productData,
      id: productId,
      updatedAt: new Date().toISOString()
    }
  }

  async deleteProduct(token, _productId) {
    await new Promise(resolve => setTimeout(resolve, 500))
    
    if (!token) {
      throw new Error('未授权访问')
    }
    
    return { success: true, message: '商品删除成功' }
  }

  async getOrders(token) {
    await new Promise(resolve => setTimeout(resolve, 500))
    
    if (!token) {
      throw new Error('未授权访问')
    }
    
    // 模拟订单数据
    return [
      { id: 1001, userId: 2, userName: '普通用户', total: 5999, status: 'paid', createdAt: '2024-01-10' },
      { id: 1002, userId: 2, userName: '普通用户', total: 1899, status: 'shipped', createdAt: '2024-01-11' },
      { id: 1003, userId: 3, userName: '测试用户', total: 12999, status: 'pending', createdAt: '2024-01-12' }
    ]
  }

  async updateOrderStatus(token, _orderId, _status) {
    await new Promise(resolve => setTimeout(resolve, 500))
    
    if (!token) {
      throw new Error('未授权访问')
    }
    
    return { success: true, message: '订单状态更新成功' }
  }

  async getUsers(token) {
    await new Promise(resolve => setTimeout(resolve, 500))
    
    if (!token) {
      throw new Error('未授权访问')
    }
    
    // 模拟用户数据
    return [
      { id: 1, email: 'admin@example.com', name: '管理员', role: 'admin', status: 'active', createdAt: '2024-01-01' },
      { id: 2, email: 'user@example.com', name: '普通用户', role: 'user', status: 'active', createdAt: '2024-01-02' },
      { id: 3, email: 'test@example.com', name: '测试用户', role: 'user', status: 'inactive', createdAt: '2024-01-03' }
    ]
  }

  async updateUserRole(token, _userId, _role) {
    await new Promise(resolve => setTimeout(resolve, 500))
    
    if (!token) {
      throw new Error('未授权访问')
    }
    
    return { success: true, message: '用户角色更新成功' }
  }

  async updateUserStatus(token, _userId, _status) {
    await new Promise(resolve => setTimeout(resolve, 500))
    
    if (!token) {
      throw new Error('未授权访问')
    }
    
    return { success: true, message: '用户状态更新成功' }
  }
}

export const adminService = new AdminService()